package com.ego.dubbo.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.dubbo.service.TbContentDubboService;
import com.ego.mapper.TbContentMapper;
import com.ego.pojo.TbContent;
import com.ego.pojo.TbContentExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TbContentDubboServiceImpl implements TbContentDubboService {
    @Autowired
    private TbContentMapper tbContentMapper;

    /**
     * 不同分类下的模糊查询
     * @param categoryId
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public List<TbContent> selectByPage(Long categoryId, int pageNumber, int pageSize) {

        PageHelper.startPage(pageNumber,pageSize);
        TbContentExample example = new TbContentExample();
        //如果category=0，查询全部，否则根据传过来主键查询
        if (categoryId !=0){
            example.createCriteria().andCategoryIdEqualTo(categoryId);
        }
        List<TbContent> list = tbContentMapper.selectByExampleWithBLOBs(example);
        PageInfo<TbContent> pi = new PageInfo<>(list);
        return pi.getList();
    }


    /**
     * 根据类目查询该类目的数量
     * @param categoryId
     * @return
     */
    @Override
    public long selectCountByCategoryId(Long categoryId) {
        TbContentExample example = new TbContentExample();
        //如果category=0，查询全部，否则根据传过来主键查询
        if (categoryId !=0){
            example.createCriteria().andCategoryIdEqualTo(categoryId);
        }
        return tbContentMapper.countByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(TbContent tbContent) throws DaoException {
        int index = this.tbContentMapper.insert(tbContent);
        if (index ==1){
            return 1;
        }
        throw new DaoException("新增失败");
    }

    @Override
    //@Transactional(rollbackFor = Exception.class)
    public int update(TbContent tbContent) throws DaoException {
        int index2 = this.tbContentMapper.updateByPrimaryKeySelective(tbContent);
        if (index2 ==1){
            return 1;
        }
        throw new DaoException("更改失败");
    }

    @Override
    public int deleteByIds(long[] ids) throws DaoException {
        int index =0;
        for (long id:ids){
            index+= this.tbContentMapper.deleteByPrimaryKey(id);
        }
        if (index == ids.length){
            return 1;
        }
        throw new DaoException("删除失败");
    }

    /**
     * 查询全部，按照更新时间降序查询
     * @param categoryId
     * @return
     */
    @Override
    public List<TbContent> selectAllByCategoryId(Long categoryId) {
        TbContentExample example = new TbContentExample();
        example.createCriteria().andCategoryIdEqualTo(categoryId);
        example.setOrderByClause("updated desc");
        return this.tbContentMapper.selectByExample(example);
    }


}
